mustangnews

The purpose of the mustangnews package is to assist The Hill, the data journalism team at Mustang News (Cal Poly’s newspaper), in data collection and visualization.

Installation

You can install the released version of mustangnews from CRAN with:

install.packages("mustangnews")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("srhen/mustangnews")

This package contains two types of functions, those that collect/process data and those that visualize data

Data Visualization Functions

While the ggplot2 and leaflet packages are excellent for making graphs and maps, it’s syntax can be a bit confusing for R beginners. These functions simplify the syntax so graphs can be made quickly and easily. For more information and to see how the data should be formatted, use the ?simple_bar() or ?simple_line() functions.

Bar graphs

To make a basic bar graph, use the function simple_bar()

library(mustangnews)
simple_bar(ad_spending_overall,
           title = "Spending on Ads during Presidential Elections")

We can add in more variables to our data set to add more information to our graph. A third variable uses color to denote the groups, and a fourth creates side by side plots

simple_bar(ad_spending_total, title = "Spending on Ads during Presidential Elections")

simple_bar(ad_support_oppose, title = "Spending on Ads during Presidential Elections")

You can also change settings so the bars are stacked on top of each other for a more condensed graph and even scale by percentage.

simple_bar(ad_spending_total, title = "Spending on Ads during Presidential Elections", stacked = TRUE)

names(ad_spending_total)[2] <- "Percentage of Ad Spending"
ad_spending <- simple_bar(ad_spending_total, title = "Spending on Ads during Presidential Elections", percent = TRUE)
ad_spending

You might have noticed that the default colors chosen are less than ideal. Two color palettes are included in this package, one with Mustang News colors and one with Cal Poly colors. You can see the colors in the two palettes by using the see_colors() function

see_colors("mn")

## NULL
see_colors("cp")

## NULL

The colors for the Democratic and Republican parties can be changed the colors we associate with them using the set_colors() function.

ad_spending <- ad_spending +
  set_colors(palette = "mn", graph = "bar", navy, red)
ad_spending

And finally, to get this graph web or print ready, add the corresponding theme to change the fonts and background.

ad_spending + web_theme()

ad_spending + print_theme()

Line graphs

Line graphs can be made in a very similar manner to bar graphs. Each additional variable adds a layer of complexity to the graph.

simple_line(cal_poly_stalking,
            title = "Stalking Occurrences at Cal Poly: 2014-18")

simple_line(stalking_3,
            title = "Stalking Occurrences at Selected CA Universities")

assault <- simple_line(sexual_assault_4,
            title = "Sexual Assault at Occurrences Selected CA Universities")
assault

A fully print ready graph could be produced by the following code

assault <- simple_line(sexual_assault_4,
            title = "Sexual Crimes on University Campuses",
            caption = "Source: US Dept. of Ed.",
            grid = T) +
  print_theme()
assault

Choropleths (Maps)

Choropleths are maps that are shaded according to the value of a variable in that area. state_map() creates a choropleth of the US by state, and county_map() creates a choropleth of the US by county.

state_map(life_exp)
county_map(median_income)

To map a single state by county, specify which state you would like

county_map(median_income, state = "PA")

If some of the counties have missing data, use the check_county_names() function to compare the county names in your data set to those in the function’s data set for any misspelling or spacing errors. Fix these within your own data set to match what is in the functions and the counties should show up

check_county_names(median_income, state = "PA")
## Joining, by = c("county", "state_name")
## Joining, by = c("county", "state_name")
##   function_counties function_state your_counties   your_state
## 1            McKean   Pennsylvania       Mc Kean Pennsylvania
median_income[median_income$Name == "Mc Kean County", "Name"] <- "McKean County"
county_map(median_income, state = "PA")

Data Collection Functions

Police Activity Logs

Police activity log data is often of interest. These logs are posted online daily. SLO PD only posts the previous day’s log (or the log of Thur-Sun on Monday) while the Cal Poly UPD site has all calls for the past 60 days.

To get the current logs and format them as a data frame use the slopd_log() and upd_log() functions. The slopd_log() can also read a txt file that was downloaded from the website and get latitude and longitude for the addresses in the file if an API code (which can be obtained here) is supplied. You can specify your starting and ending dates for the upd_log() file as well, just keep in mind that they need to be within the last 60 days.

head(slopd_log())
##       Incid     Date            Received          Dispatched
## 1 200610014 06/10/20 2020-06-10 07:02:00 2020-06-10 07:20:00
## 2 200610015 06/10/20 2020-06-10 07:41:00 2020-06-10 07:43:00
## 3 200610016 06/10/20 2020-06-10 07:44:00 2020-06-10 07:50:00
## 4 200610017 06/10/20 2020-06-10 07:46:00 2020-06-10 07:49:00
## 5 200610018 06/10/20 2020-06-10 08:05:00 2020-06-10 08:07:00
## 6 200610019 06/10/20 2020-06-10 08:09:00 2020-06-10 08:43:00
##               Arrived             Cleared          Type Location
## 1 2020-06-10 07:24:00 2020-06-10 07:26:00     Vandalism      PN1
## 2 2020-06-10 07:48:00 2020-06-10 07:59:00    Disorderly      PN7
## 3 2020-06-10 08:05:00 2020-06-10 08:22:00     Loitering     PN12
## 4 2020-06-10 07:59:00 2020-06-10 08:02:00 Alarm Audible      PN5
## 5 2020-06-10 08:11:00 2020-06-10 08:23:00     Loitering      PN5
## 6 2020-06-10 08:51:00 2020-06-10 08:55:00   Trespassing      PN9
##   As_Observed                             Address Apt_num    Location_Info
## 1             753 PASATIEMPO, San Luis Obispo, CA    <NA>             <NA>
## 2               1911 JOHNSON, San Luis Obispo, CA    <NA>  FRENCH HOSPITAL
## 3             3971 HIGUERA S, San Luis Obispo, CA    <NA>        STARBUCKS
## 4               886 MONTEREY, San Luis Obispo, CA    <NA>          H AND M
## 5 Trespassing      692 MARSH, San Luis Obispo, CA    <NA>              711
## 6                254 GRANADA, San Luis Obispo, CA    <NA> TACKLE WAREHOUSE
##   Grid             City   Clearance_Code Responsible_Officer
## 1 H-04 San Luis Obisp          No Report            Tyler, B
## 2 L-10                   Field Interview            Tyler, B
## 3 I-17            San          No Report       Villanueva, I
## 4 K-09        San Lui   ALARM-HUMAN ERRO          Sanchez, M
## 5 J-10 San Luis Obisp    Field Interview           Newton, J
## 6 J-16                   Gone on Arrival          Donovan, M
##                                             Units               Des disp
## 1                                           4263   Completed call   <NA>
## 2                      4263                ,4255    Completed call  <NA>
## 3                      4234                ,4205    Completed call   NR 
## 4                      4262                ,4246   Completed call   <NA>
## 5 4246                ,4262                ,4263   Completed call   <NA>
## 6                      4255                ,4263   Completed call   <NA>
##    clr   oc Call
## 1 <NA> <NA>  21l
## 2 <NA> <NA>  32l
## 3  NR  <NA>  27l
## 4 <NA> <NA>  28l
## 5 <NA> <NA>  30l
## 6 <NA> <NA>  34l
##                                                        Call_Comments   MDC
## 1            VEH EGGED OVERNIGHT, ALONG WITH 40 OTHERS ON THE STREET  TRUE
## 2                                                    *REOPENED CALL* FALSE
## 3     TRANSIENT FEMALE W/ LARGE AMOUNT OF PROPERTY REFUSING TO LEAVE FALSE
## 4  EMP ENTRY DOOR, EMP ENTRY MOTION, LADIES CASH WRAP MOTION, LADIES  TRUE
## 5                                    MALE REFUSING TO LEAVE THE BUSN  TRUE
## 6                                 2ND HAND INFO FROM WORKMAN ON SITE  TRUE
head(upd_log(start = "2020-06-05", end = "2020-06-06"))
##    Date_Time_Reported Occurance_Began Occurance_Ended              CAD
## 1 2020-06-06 23:12:00            <NA>            <NA>  20-06-06-013586
## 2 2020-06-06 22:32:00            <NA>            <NA>  20-06-06-013584
## 3 2020-06-06 21:50:00            <NA>            <NA>  20-06-06-013583
## 4 2020-06-06 21:16:00            <NA>            <NA>  20-06-06-013581
## 5 2020-06-06 21:02:00            <NA>            <NA>  20-06-06-013580
## 6 2020-06-06 21:01:00            <NA>            <NA>  20-06-06-013579
##   Report             Type        Subtype1     Subtype2
## 1         PATROL ACTIVITY    Patrol Check         <NA>
## 2                 MEDICAL     MEDICAL AID ALCOHOL/DRUG
## 3         PATROL ACTIVITY    Patrol Check         <NA>
## 4          CITIZEN ASSIST Citizen Contact         <NA>
## 5                 VEHICLE    Vehicle Stop         <NA>
## 6                  ANIMAL     Animal Call         <NA>
##                 Disposition                 Location      Area
## 1                In Service         Ytt Area f Dorms       Ytt
## 2                  TOT SLPD        819 Pasatiempo Dr On Campus
## 3                In Service Poly Canyon Village Area       PCV
## 4 Subject(s) Advised/Warned         Ytt Area f Dorms       Ytt
## 5     Driver Warned/Advised             Fh Jwo Calif On Campus
## 6                In Service       Dairy Science Unit On Campus
##                  Area2
## 1       Freshman Dorms
## 2            On Campus
## 3 On Campus Apartments
## 4       Freshman Dorms
## 5            On Campus
## 6            On Campus

Greek Life Sanctions

It can be useful to keep an eye on the Greek Life sanctions page so that you know which chapters are on probation/suspensions and can see how those have changed since the last time the page was checked.

To get the current sanctions, use the get_gl_sanctions() function.

get_gl_sanctions()
##   Letters            Chapter
## 1     ΛΣΓ Lambda Sigma Gamma
## 2      ϴX          Theta Chi
## 3    <NA> No chapters listed
##                                                                                                                                                                                                                                                                                                                                                                                                   Addtional_Info_1
## 1 Probation until the end of Spring 2021 quarter. During the probationary period, the club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of hazing and conspiracy to haze, Violation of policies, and Violation of Law
## 2          Probation until the end of Spring 2021 quarter. During the probationary period, Theta Chi club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of Alcohol, Violation of Health & Safety, and Violation of Law
## 3                                                                                                                                                                                                                                                                                                                                                                                                             <NA>
##                      Addtional_Info_2 Addtional_Info_3 Addtional_Info_4
## 1                                <NA>               NA               NA
## 2 Additional educational requirements               NA               NA
## 3                                <NA>               NA               NA
##               Section
## 1        On Sanctions
## 2        On Sanctions
## 3 Open Investigations

If you would like to save this file to your current working directory, you can do that as well by specifying get_gl_sanctions(save = TRUE)

The comp_gl_sanctions() will compare the current sanctions to an old sanction file that you have saved, such as this one from April 25, 2020, or can compare two old sanction files as well. Anything in red denotes information that was in the old file that has since been removed and anything in green has been added to the site since your old file was obtained.

comp_gl_sanctions(old_sanctions)
Chapter chng_type Letters Addtional_Info_1 Addtional_Info_2 Addtional_Info_3 Section
Alpha Sigma Phi - AΣΦ Alpha Sigma Phi is placed on alcohol restriction effective immediately and will not be authorized to host any events that include alcohol through the end of Spring Quarter 2020. Alcohol restriction does not prohibit a chapter from participating in Intramural Sports, recruitment activities, philanthropic events, or alumni related events that do not have alcohol present. Alpha Sigma Phi is placed on probation effective immediately through the end of Spring Quarter 2021. During the probationary period, Alpha Sigma Phi club/organization is expected to demonstrate exemplary compliance with University policies. Violation of alcohol, Violation of health and safety, Violation of policies, Violations of laws and Violation of party registration. Additional educational requirements On Sanctions
Beta Theta Pi - BϴΠ Beta Theta Pi is placed on alcohol restriction effective immediately and will not be authorized to host any events that include alcohol through the end of Spring Quarter 2020. Alcohol restriction does not prohibit a chapter from participating in Intramural Sports, recruitment activities, philanthropic events, or alumni related events that do not have alcohol present. Beta Theta Pi is placed on probation effective immediately through the end of Spring Quarter 2020. During the probationary period, Beta Theat Pi club/organization is expected to demonstrate exemplary compliance with University policies. Violation of health & safety, violation of alcohol, violations of policies & law, violation of event registration procedures. Additional educational sanctions On Sanctions
Kappa Sigma - Probation through Spring 2020. Kappa Sigma is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete.Violation of Alcohol, Violation of Hazing & Conspiracy to Haze, Violation of Health & Safety, Violation of University Policy, Violation of Law. Additional educational requirements On Sanctions
Lambda Sigma Gamma + ΛΣΓ Probation until the end of Spring 2021 quarter. During the probationary period, the club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of hazing and conspiracy to haze, Violation of policies, and Violation of Law On Sanctions
Lambda Sigma Gamma - ΛΣΓ Suspension effective immediately until the end of spring quarter 2020. Alleged violation of hazing and conspiracy to haze, violations of policies, violation of law. Suspension allows the organization to only host: chapter meetings (one per week), meetings for educational purposes (e.g. workshops, discussions, guest speakers), one philanthropic event during winter quarter 2020. Probation until the end of Spring 2021 quarter. During the probationary period, the club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of hazing and conspiracy to haze, Violation of policies, and Violation of Law On Sanctions
Phi Sigma Kappa - ΦΣK Phi Sigma Kappa is placed on social probation effective immediately and will not be authorized to host any social events that include alcohol through the end of the Spring Quarter 2020. Social probation does not restrict a chapter from participating in Intramural Sports, recruitment activities, philanthropic events, or alumni related events that do not have alcohol present. Violation of alcohol, Violation of health and safety, Violation of policies, Violations of laws and Violation of party registration On Sanctions
Sigma Nu - ΣN Social probation effective immediately until the end of Spring 2020. Social probation does not restrict a chapter from participating in Intramural Sports, recruitment activities, philanthropic events, or alumni related events that do not have alcohol present. Violation of Alcohol, Health & Safety, Violations of Law and University Policy and Party Registration Procedure Additional educational requirements On Sanctions
Sigma Pi - ΣΠ Probation through Spring 2020. During the probationary period, Sigma Pi is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete.Violation of Registered Student Organization Code of Conduct, Violation of health and safety, Violation of alcohol use, Violations of law, Violation of hazing & conspiracy to haze ​​​​​ Additional educational requirements   On Sanctions
Theta Chi + ϴX Probation until the end of Spring 2021 quarter. During the probationary period, Theta Chi club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of Alcohol, Violation of Health & Safety, and Violation of Law Additional educational requirements On Sanctions
Theta Chi - ϴX Suspension effective immediately until the end of Winter 2020. Suspension allows the organization to only host: chapter meetings (one per week), meetings for educational purposes (e.g. workshops, discussions, guest speakers),one philanthropic event during winter quarter 2020. Alcohol restriction effective immediately until the end of Spring 2020 quarter. Probation until the end of Spring 2021 quarter. During the probationary period, Theta Chi club/organization is expected to demonstrate exemplary compliance with University policies. This includes the successful and timely completion of any additional sanction(s) that the club/organization may be required to complete. Violation of Alcohol, Violation of Health & Safety, and Violation of Law Additional educational requirements On Sanctions